/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;

/**
 *
 * @author Jacob Morgan
 */
public class AssetManager {
    private static HashMap<String, BufferedImage> images = new HashMap<String, BufferedImage>(128);
    private static HashMap<String, AudioClip> clips = new HashMap<String, AudioClip>(128);
    private static BufferedImage pixel;
    static {
        pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    }
    public static void list()
    {
        for(String s : images.keySet())
        {
            System.out.println(s);
        }
        for(String s : clips.keySet())
        {
            System.out.println(s);
        }
    }
    public static BufferedImage loadImage(String name, String path, boolean useQuickTransparency) throws IOException
    {
        if(Main.noView)
            return pixel;
        if(images.containsKey(name))
            return images.get(name);
        URL url = null;
        if(path.startsWith("resource://"))
            url = AssetManager.class.getResource(path.replace("resource://", "/net/resources/"));
        else if(path.startsWith("http://"))
            url = new URL(path);
        else if(path.startsWith("/"))
            url = AssetManager.class.getResource(path);
        else
            url = AssetManager.class.getResource("/net/resources/"+path);
        if(url == null)
            throw new IOException("Resource not found: "+path);
        InputStream stream = url.openStream();
        BufferedImage finalImage = ImageIO.read(stream);
        BufferedImage screenImage = Util.getGraphicsConfiguration().createCompatibleImage(finalImage.getWidth(), finalImage.getHeight(), Transparency.TRANSLUCENT);
        screenImage.getGraphics().drawImage(finalImage, 0, 0, null);
        stream.close();
        images.put(name, screenImage);
        return screenImage;
    }
    public static BufferedImage loadImage(String name, String path) throws IOException
    {
        return loadImage(name, path, false);
    }
    public static AudioClip loadSound(String name, String path) throws IOException
    {
        if(clips.containsKey(name))
            return clips.get(name);
        URL url = null;
        if(path.startsWith("resource://"))
            url = AssetManager.class.getResource(path.replace("resource://", "/net/resources/"));
        else if(path.startsWith("http://"))
            url = new URL(path);
        else if(path.startsWith("/"))
            url = AssetManager.class.getResource(path);
        else
            url = AssetManager.class.getResource("/net/resources/"+path);
        if(url == null)
            throw new IOException("Resource not found: "+path);
        AudioClip sound = Applet.newAudioClip(url);
        clips.put(name, sound);
        return sound;
    }
    public static BufferedImage getImage(String n)
    {
        return images.get(n);
    }
    public static AudioClip getSound(String n)
    {
        return clips.get(n);
    }
    public static BufferedImage tileTexture(BufferedImage texture, int width, int height, boolean doX, boolean doY, boolean flipX, boolean flipY)
    {
        if(Main.noView)
            return pixel;
        GraphicsConfiguration gc = Util.getGraphicsConfiguration();
        BufferedImage result = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
        int tilesX = (int) Math.ceil(width/texture.getWidth()) + 1;
        int tilesY = (int) Math.ceil(height/texture.getHeight()) + 1;
        int imgX = 0;
        int imgY = 0;
        Graphics2D g = (Graphics2D) result.getGraphics();
        if(doX || doY)
        {
            for(int x = 0; x < tilesX; x++)
            {
                for(int y = 0; y < tilesY; y++)
                {

                    if(flipX && flipY)
                        g.drawImage(texture, imgX, imgY, imgX + texture.getWidth(), imgY + texture.getHeight(), texture.getWidth(), texture.getHeight(), 0, 0, null);
                    else if(flipX)
                        g.drawImage(texture, imgX, imgY, imgX + texture.getWidth(), imgY + texture.getHeight(), texture.getWidth(), 0, 0, texture.getHeight(), null);
                    else if(flipY)
                        g.drawImage(texture, imgX, imgY, imgX + texture.getWidth(), imgY + texture.getHeight(), 0, texture.getHeight(), texture.getWidth(), 0, null);
                    else
                        g.drawImage(texture, imgX, imgY, imgX + texture.getWidth(), imgY + texture.getHeight(), 0, 0, texture.getWidth(), texture.getHeight(), null);
                    if(doX)
                        imgX += texture.getWidth();
                    if(doX && doY && imgX > width || !doX)
                    {
                        imgX = 0;
                        imgY += texture.getHeight();
                    }
                }
            }
        }
        else
        {
                   if(flipX && flipY)
                        g.drawImage(texture, imgX, imgY, imgX + width, imgY + height, texture.getWidth(), texture.getHeight(), 0, 0, null);
                    else if(flipX)
                        g.drawImage(texture, imgX, imgY, imgX + width, imgY + height, texture.getWidth(), 0, 0, texture.getHeight(), null);
                    else if(flipY)
                        g.drawImage(texture, imgX, imgY, imgX + width, imgY + height, 0, texture.getHeight(), texture.getWidth(), 0, null);
                    else
                        g.drawImage(texture, imgX, imgY, imgX + width, imgY + height, 0, 0, texture.getWidth(), texture.getHeight(), null);

        }
        return result;
    }
    
    public static Font loadFont(String name, int size)
    {
        InputStream in = AssetManager.class.getResourceAsStream(name);
        Font f = null;
        try   
        {
            f = Font.createFont(Font.TRUETYPE_FONT, in).deriveFont(-1, size);
            return f;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }
    }
}
